package com.ljpz.manage.service;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljpz.manage.pojo.BasePojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by liujunchen on 2016/11/28.
 */
public      class   BaseService<T extends BasePojo> {
//    public  abstract Mapper<T> getMapper() ;
    @Autowired
    private  Mapper<T> mapper;
    /**
     *
     *
     1、	queryById
     2、	queryAll
     3、	queryOne
     4、	queryListByWhere
     5、	queryPageListByWhere
     6、	save
     7、	update
     8、	deleteById
     9、	deleteByIds
     10、	deleteByWhere

     */
    public  T queryById(Long id ){
        return    mapper.selectByPrimaryKey(id) ;
    }
    public List<T>queryAll(){
        return  mapper.select(null) ;
    }
    public  T queryOne(T record ){
        return  mapper.selectOne(record);
    }
    public  List<T> queryListByWhere(T record){
        return   mapper.select(record) ;
    }

    /**
     *
     * 根据条件分页查询
     * @param page
     * @param rows
     * @param record
     * @return
     */
    public PageInfo<T>queryPageListByWhere(Integer page , Integer rows ,T record){
        //设置分页条件
        PageHelper.startPage(page,rows) ;
        List<T> list = this.queryListByWhere(record) ;
        return  new PageInfo<T>(list)  ;
    }

    /**
     *
     *选择不为null 的字段作为插入数据的字段来插入数据
     * @param record
     * @return
     */
    public   Integer   save (T  record ){
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return  mapper.insert(record);
    }
    public  Integer saveSelective(T  t ){
        t.setCreated(new Date());
        t.setUpdated(t.getCreated());
        return  mapper.insertSelective( t )  ;
    }
    public   Integer   update  (T  record ){
        record.setUpdated(new Date());
        return  mapper.updateByPrimaryKey(record);
    }
    public  Integer updateSelective(T  t ){
        t.setUpdated(new Date());
        t.setCreated(null);
        return  mapper.updateByPrimaryKeySelective( t )  ;
    }
    public   Integer deleteById(Long id  ){
        return  mapper.deleteByPrimaryKey(id );
    }
    public  Integer deleteByIds(List<Object> ids  , Class<T>  clazz  , String property){
        Example example = new Example(clazz);
        //设置条件
        example.createCriteria().andIn(property, ids);
        return  mapper.deleteByExample(example )  ;
    }
    public  Integer deleteByWhere(T t ){
        return   mapper.delete(t)   ;
    }
}
